﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Metadata.Edm;
using System.Data.Objects.DataClasses;

namespace SIMCRepositorio
{
    public static partial class EntityFrameworkExtensionMethods
    {
        public static string GetEntitySetName(this ObjectQuery objectQuery, ObjectContext context)
        {
            string entityTypeName = GetEntityTypeName(objectQuery);
            return GetEntitySetName(entityTypeName, context);
        }

        public static string GetEntityTypeName(this ObjectQuery objectQuery)
        {
            return objectQuery.GetResultType().EdmType.Name;
        }

        public static string GetEntitySetName(string entityTypeName, ObjectContext context)
        {
            if (context == null)
            {
                return "";
            }

            var container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
            string entitySetName = (from meta in container.BaseEntitySets
                                    where meta.ElementType.Name == entityTypeName
                                    select meta.Name).FirstOrDefault();

            return entitySetName ?? "";
        }

        public static string GetEntitySetName(this EntityObject entityObject, ObjectContext context)
        {
            if (entityObject.EntityKey != null)
            {
                return entityObject.EntityKey.EntitySetName;
            }
            string entityTypeName = GetEntityTypeName(entityObject);
            return GetEntitySetName(entityTypeName, context);
        }

        public static string GetEntityTypeName(this EntityObject entityObject)
        {
            return entityObject.GetType().Name;
        }

        public static ObjectQuery<T> GetEntitySet<T>(this T entityObject, ObjectContext context)
          where T : EntityObject
        {
            if (context == null)
            {
                return null;
            }
            string entitySetName = GetEntitySetName(entityObject, context);
            string entityContainerName = context.DefaultContainerName;
            if (String.IsNullOrEmpty(entitySetName) || String.IsNullOrEmpty(entityContainerName))
            {
                return null;
            }
            string eSql = String.Format("SELECT VALUE a FROM {0}.{1} AS a", entityContainerName, entitySetName);
            return context.CreateQuery<T>(eSql);
        }

        public static string GetEntitySetName<TEntity>(this EntityCollection<TEntity> entityCollection, ObjectContext context)
          where TEntity : class, IEntityWithRelationships
        {
            string entityTypeName = GetEntityTypeName(entityCollection);

            return GetEntitySetName(entityTypeName, context);
        }

        public static string GetEntityTypeName<TEntity>(this EntityCollection<TEntity> entityCollection)
          where TEntity : class, IEntityWithRelationships
        {
            return typeof(TEntity).Name;
        }
    }
}
